package bubbleindex;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author ttrott
 */
public class ExportData {
    
    /**
     * WriteCSV writes the results to FileName
     * @param Results
     * @param PERIODS
     * @param FileName
     * @param UPDATE
     * @throws IOException 
     */
    public static void WriteCSV(List<Float> Results, int PERIODS, String FileName, 
            boolean UPDATE) throws IOException {
           
        FileWriter writer;

        try {
            writer = new FileWriter(BubbleIndex.SavePath + Indices.filePathSymbol + 
                    FileName, UPDATE);
            
            if(!UPDATE) {
                addHeader(writer);
            }
                            
            for (int i = 0; i < Results.size(); i++) {

                writer.append(Integer.toString(PERIODS - Results.size() + 
                                    i + 1));               
                writer.append(',');
                writer.append(String.valueOf(Results.get(i)));
                writer.append(',');
                writer.append(BubbleIndex.DailyPriceDateList.get(BubbleIndex.DailyPriceDateList.size() - 
                        Results.size() + i));               
                writer.append('\n');
            }            

            writer.flush();
            writer.close();
        
        } catch (IOException ex) { 
            System.out.println("IOException. Code 010.");
        }        
    }
     
    /**
	 * addHeader appends a header to the output file
	 * @param writer the pointer to the filewriter stream
	 * @throws IOException
	 * @return void
	 */
    private static void addHeader(FileWriter writer) throws IOException { 
        writer.append("Period Number");
        writer.append(',');
        writer.append("Value");
        writer.append(',');
        writer.append("Date");
        writer.append('\n');   
    }
}